﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
//using Microsoft.Xna.Framework.Net;
//using Microsoft.Xna.Framework.Storage;
//using xnaExtras; //<-------FINNS INTE FÖR WEBSIDAN ÄR BORTA >.<


namespace SpriteDisplay
{
    /// <summary>
    /// Player klassen tar han om det som har med spelaren att göra. Ex, antal liv, rörelse, sikte.
    /// </summary>
    class Player
    {
        Vector2 position;
        Texture2D playerSprite;

        Vector2 spriteOrigin;
        //for window bounds
        int windowWidth, windowHeight;
        // Up/Down speed
        float velocity = 0.0f;

        //for HUD -------------
        int lives = 5;

        public int Lives
        {
            set { lives = value; }
            get { return lives; }
        }

        //Mouse tracking %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        float angle;
        Vector2 direction;
        //Vector position;
        Vector2 mouse_pos;

        //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

        //Collision detection ---------------------------------
        float radius = 50.0f;

        float blinkTime = 0.0f;
        float blinkTimeTotal = 0.0f;
        bool blinkOn = false;
        bool recoveringActive = false;
        const float recoverLength = 3.0f;

        public bool CollisionTest(Vector2 point, float radius)
        {
            if ((point - position).Length() < this.radius + radius)
            {
                if (!recoveringActive)
                {
                    lives--; //<---for HUD
                    recoveringActive = true;
                    blinkTimeTotal = 0.0f;
                }
                return true;
            }
            return false;
        }
        //-----------------------------------------------------

        //This method is called when the Player is created (Constructor)
        public Player(GraphicsDevice device, Vector2 position, Texture2D sprite)
        {
            //The position that is passed in is now set to the position above
            this.position = position;

            //Set the Texture2D
            playerSprite = sprite;

            //Setup origin
            spriteOrigin.X = (float)playerSprite.Width / 2.0f;
            spriteOrigin.Y = (float)playerSprite.Height / 2.0f;

            //Set window dimensions
            windowHeight = device.Viewport.Height;
            windowWidth = device.Viewport.Width;

            MouseState mouse = Mouse.GetState();
            mouse_pos.X = mouse.X;
            mouse_pos.Y = mouse.Y;
        }

        //Draw the player
        public void Draw(SpriteBatch batch)
        {
            if (!recoveringActive || (recoveringActive && !blinkOn))
                batch.Draw(playerSprite, Position, null, Color.White, Angle, new Vector2(spriteOrigin.X, spriteOrigin.Y), 1.0f, SpriteEffects.None, 1.0f);   
        }

        //Update
        public void Update(GameTime gameTime)
        {
            MouseState mouse = Mouse.GetState();
            mouse_pos.X = mouse.X;
            mouse_pos.Y = mouse.Y;
            CalculateAngleAndDirection(Position, mouse_pos, out direction, out angle);

            position.Y += velocity * (float)gameTime.ElapsedGameTime.TotalSeconds;
            velocity *= 0.95f;

            if (position.Y < 0) position.Y = 0.0f;
            if (position.Y > windowHeight) position.Y = windowHeight;
            if (position.X < 0) position.X = 0.0f;
            if (position.X > windowWidth) position.X = windowWidth;

            //Collision detection -------------------------
            if (recoveringActive)
            {
                const float blinkTimeSlice = 1.0f / 15.0f;
                float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
                blinkTime += elapsed;
                blinkTimeTotal += elapsed;
                if (blinkTimeTotal > recoverLength)
                    recoveringActive = false;
                else
                {
                    if (blinkTime > blinkTimeSlice)
                    {
                        blinkOn = !blinkOn;
                        blinkTime = 0.0f;
                    }
                }
            }

            //---------------------------------------------
        }

        //These match up with the Arrow keys
        public void MoveUp()
        {
            position.Y -= 6.0f;
        }

        public void MoveDown()
        {
            position.Y += 6.0f;
        }

        public void MoveRight()
        {
            position.X += 6.0f;
        }

        public void MoveLeft()
        {
            position.X -= 6.0f;
        }

        public Vector2 Position 
        {
            get
            {
                return position;
            }
        }

        public Vector2 Direction
        {
            get
            {
                return direction;
            }
        }

        public float Angle
        {
            get
            {
                return angle;
            }
        }

        public void CalculateAngleAndDirection(Vector2 from, Vector2 to, out Vector2 direction, out float angle)
        {
            direction = to - from; //get the direction vector
            direction.Normalize(); //make it a unit vector
            angle = (float)Math.Atan2(-direction.X, direction.Y);
        }
    }
}
